﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Workflow.Common.ExcelHelp;
using Workflow.Common.Tools;
using Workflow.IService;
using Workflow.Model;
using Workflow.Resource;
using Workflow.Service;
using Workflow.Web.Common;
using User = Workflow.Service.Menu.Models.Maintain.User;

namespace Workflow.Web.Controllers
{
	[Localization]
	[UserAuthorize]
	public class HolidayController : Controller
	{
		//
		// GET: /Holiday/

		public ITIdleDateService TIdleDateService { get; set; }

		public ActionResult HolidayView()
		{
			return View();
		}

		public ActionResult GetHoliday()
		{
			string s = Request["page"] ?? "1";
			string r = Request["rows"];
			string sortfield = Request["sort"];
			string order = Request["order"];
			if (sortfield == null)
				sortfield = "IdleDate";
			if (r == null)
				r = "10";
			long count;

			var users = TIdleDateService.GetTIdleDatesByPage(out count, int.Parse(s), int.Parse(r));

			var list = users.Select(entity => new
			{
				entity.Id,
				IdleDate = entity.IdleDate.ToString("yyyy/MM/dd",System.Globalization.DateTimeFormatInfo.InvariantInfo)
			});

			var result = new { total = count, rows = list.ToList() };

			return this.Json(result, JsonRequestBehavior.AllowGet);
		}

		public ActionResult Detail(string id)
		{
			TIdleDate entity = null;
			if (id != null)
			{
				entity = this.TIdleDateService.GetTIdleDateById(Int64.Parse(id));
			}
			entity = entity ?? new TIdleDate()
			{
				IdleDate = ToolsHelper.GetTimeZoneNow()
			};

			this.ViewData["entity"] = entity;

			return View();
		}

		public ActionResult SaveHoliday(Int64 Id, DateTime IdleDate)
		{
			try
			{
				if (Id == 0)
				{
					if(TIdleDateService.GetTIdleDatesByDate(IdleDate).Count==0)
					{

						TIdleDate entity = new TIdleDate();
						
						entity.IdleDate = IdleDate;

						this.TIdleDateService.SaveOrUpdateTIdleDate(entity);
					}
					else
					{
						return Json(new { Success = false, Message = Global.SaveFalse }, "text/html", JsonRequestBehavior.AllowGet);

					}
				}
				else
				{
					if (TIdleDateService.GetTIdleDatesByDate(IdleDate).Count == 0)
					{
						var model = this.TIdleDateService.GetTIdleDateById(Id);

						model.IdleDate = IdleDate;

						this.TIdleDateService.SaveOrUpdateTIdleDate(model);
					}
					else
					{
						return Json(new { Success = false, Message = Global.SaveFalse }, "text/html", JsonRequestBehavior.AllowGet);
					}
				}

				return Json(new { Success = true, Message = Global.SaveSuccess }, "text/html", JsonRequestBehavior.AllowGet);

			}
			catch (Exception ex)
			{
				return Json(new { Success = false, Message = Global.SaveFalse }, "text/html", JsonRequestBehavior.AllowGet);

			}


		}
		public ActionResult DeleteHoliday(string id)
		{
			try
			{

				if (id != null)
				{
					this.TIdleDateService.DeleteTIdleDate(Int64.Parse(id));
				}

				return Json(new { Success = true, Message = Global.DeleteSuccess }, "text/html", JsonRequestBehavior.AllowGet);

			}
			catch (Exception ex)
			{
				return Json(new { Success = false, Message = Global.DeleteFail }, "text/html", JsonRequestBehavior.AllowGet);

			}
		}
		public ActionResult Upload()
		{
			return View();
		}

		public static bool HasFile(HttpPostedFileBase file)
		{
			return (file != null && file.ContentLength > 0) ? true : false;
		}
		[HttpPost]
		public ActionResult UploadHoliday()
		{
			try
			{
				foreach (string upload in Request.Files)
				{
					if (!HasFile(Request.Files[upload])) continue;
					string path = AppDomain.CurrentDomain.BaseDirectory + "TempFiles/";
					string filename = Path.GetFileName(Request.Files[upload].FileName);

					Request.Files[upload].SaveAs(Path.Combine(path, filename));

					DataTable dt = ExcelHelper.ImportFromExcel(Path.Combine(path, filename));

					TIdleDateService.SaveTIdleDateList(dt);

					System.IO.File.Delete(Path.Combine(path, filename));
				}

				return Json(new { Success = true, Message = Global.UploadSuccess }, "text/html", JsonRequestBehavior.AllowGet);
			}
			catch (Exception ex)
			{
				return Json(new { Success = false, Message = Global.UploadFail }, "text/html", JsonRequestBehavior.AllowGet);

			}
		}

	}
}
